Page({
    data: {
        navInfo: null,
        info: null, //产品详情信息
        swiper: [], //视频、图片轮播
        loading: true,
        active: '', //映射按钮
        open: false, //展示报名弹出层
        choose: [{
                name: '特惠',
                choosed: false
            },
            // {
            //     name: '新品',
            //     choosed: false
            // }
            {
                name: '品质',
                choosed: false
            }, {
                name: '视频',
                choosed: false
            }
        ], //报名选中项的索引
        evaluateList: [], //评价列表
        evaluateScreenActive: 0, //评价筛选选中项
        evaluateScreenData: [
            { type: '全部', num: 0 },
            { type: '好评', num: 0 },
            { type: '中评', num: 0 },
            { type: '差评', num: 0 },
        ],
    },
    onLoad: function(options) {
        this.getDetail(options.product_id)
        this.setData({ active: Number(options.active) })
    },
    //获取产品详情
    getDetail(product_id) {
        this.request({
            url: '/api/product/detail',
            login: false,
            data: { product_id }
        }, res => {
            this.delay(() => {
                let swiper = []
                res.data.photo_list.forEach(item => {
                    swiper.push({
                        url: this.data.imgHost + item.photo + '!img_750',
                        type: 'image'
                    })
                })
                res.data.video && swiper.unshift({
                    url: this.data.imgHost + res.data.video,
                    type: 'video'
                })

                this.setData({ info: res.data, swiper, loading: false })
                this.getEvaluate()
                // this.getEvaluateNum()
            }, 300)
        }, () => {
            wx.showModal({
                content: '商品已下架',
                confirmColor: '#188075',
                showCancel: false,
                success(res) {
                    wx.navigateBack()
                }
            })
        })
    },
    //预览商品图片、视频
    previewMedia(e) {
        const { index } = e.mark
        wx.previewMedia({
            sources: this.data.swiper,
            current: index
        })
    },
    //编辑商品
    editGoods() {
        wx.navigateTo({
            url: `/pages/goods_add/index?id=${this.data.info.product_id}&supplier_type=${this.data.info.supplier_type}`,
            events: {
                acceptDataFromOpenedPage: () => { // 编辑成功，上个页面刷新
                    this.getDetail(this.data.info.product_id)
                    const eventChannel = this.getOpenerEventChannel()
                    eventChannel.emit('refresh')
                }
            }
        })
    },
    //修改价格
    // editPrice() {
    //     const { info } = this.data
    //     wx.navigateTo({
    //         url: '/pages/goods_edit_price/index',
    //         events: {
    //             refresh: () => {
    //                 this.getDetail(info.product_id)
    //                 const eventChannel = this.getOpenerEventChannel()
    //                 eventChannel.emit('refresh')
    //             }
    //         },
    //         success: (res) => {
    //             res.eventChannel.emit('acceptDataFromOpenerPage', { info })
    //         }
    //     })
    // },
    /**通用按钮操作 */
    operation(e) {
        const { tip, url, splice, active, back } = e.currentTarget.dataset
        wx.$dialog.confirm({
            title: '提示',
            message: `您确定${tip}吗？`,
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url,
                        method: 'POST',
                        data: { product_id: this.data.info.product_id }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: `${tip}成功`,
                            icon: 'success',
                            mask: true,
                            duration: 600
                        })
                        if (back) {
                            this.delay(() => {
                                wx.navigateBack()
                            })
                            return
                        }
                        this.ope_success(splice, active) //成功
                    }, () => {
                        resolve()
                    })
                }
                resolve(true)
            })
        })
    },
    /**操作成功，上个页面数据剪切 */
    ope_success(splice, active) {
        if (splice) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('splice', { active: active })
        }
        this.delay(() => {
            this.getDetail(this.data.info.product_id)
            active != undefined && this.setData({ active })
        })
    },
    //关闭报名专区弹出层
    closeSignup() {
        this.setData({
            open: false,
            choose: [{
                    name: '特惠',
                    choosed: false
                },
                // {
                //     name: '新品',
                //     choosed: false
                // }
                {
                    name: '品质',
                    choosed: false
                }, {
                    name: '视频',
                    choosed: false
                }
            ]
        })
    },
    //选择报名选项
    onChooseChange({ currentTarget }) {
        const { index } = currentTarget.dataset
        const { choosed } = this.data.choose[index]
        this.setData({
            choose: [{
                    name: '特惠',
                    choosed: false
                },
                //  {
                //     name: '新品',
                //     choosed: false
                // }
                {
                    name: '品质',
                    choosed: false
                }, {
                    name: '视频',
                    choosed: false
                }
            ],
            ['choose[' + index + '].choosed']: !choosed
        })
    },
    //点击确认报名
    confirm_signup() {
        const { choose } = this.data
        const type = choose.findIndex(item => {
            return item.choosed
        })
        this.closeSignup()
        if (type === -1) return
        let reqType = type
        if (type === 0) {
            reqType += 1
        } else {
            reqType += 2
        }
        this.request({
            url: '/supplier/product/activity/apply',
            method: 'POST',
            loadingTip: '报名中',
            data: {
                product_id: this.data.info.product_id,
                type: reqType
            }
        }, () => {
            wx.showToast({
                title: '报名成功',
                icon: 'success'
            })
        })
    },
    /**获取产品评价 */
    getEvaluate() {
        const { info, evaluateScreenActive } = this.data
        this.request({
            url: '/supplier/comment',
            data: {
                product_id: info.product_id,
                product_common_id: info.product_common_id,
                supplier_id: info.supplier.supplier_id,
                type: evaluateScreenActive,
                limit: 3,
                page: 1
            }
        }, res => {
            if (res.data.length > 0) this.setData({ evaluateList: res.data })
            if (evaluateScreenActive === 0) this.setData({
                ['evaluateScreenData[0].num']: res.count,
            })
        })
    },
    /**评价筛选条件改变 */
    onScreenChange(e) {
        const { index } = e.currentTarget.dataset
        if (index === this.data.evaluateScreenActive) return
        this.setData({ evaluateScreenActive: index, evaluateList: [] })
        this.getEvaluate()
    },
    /**获取评价数量 */
    // getEvaluateNum() {
    //     const { info } = this.data
    //     const url = `/supplier/comment/tongji?product_id=${info.product_id}&product_common_id=${info.product_common_id}&supplier_id=${info.supplier_id}`
    //     this.request({
    //         url,
    //     }, ({ data }) => {
    //         this.setData({
    //             ['evaluateScreenData[1].num']: data.praise_count,
    //             ['evaluateScreenData[2].num']: data.medium_count,
    //             ['evaluateScreenData[3].num']: data.negative_count,
    //         })
    //     })
    // },
    /**预览评价图片 */
    priviewEvaluate({ mark }) {
        const { index, photo_index, all } = mark
        if (photo_index == undefined && !all) return
        if (all) { //查看全部评价
            const { info } = this.data
            const options = `?product_id=${info.product_id}&product_common_id=${info.product_common_id}&supplier_id=${info.supplier.supplier_id}`
            return wx.navigateTo({
                url: '/pages/comment/index' + options
            })
        }
        const sources = this.data.evaluateList[index].photos.map(item => {
            return this.data.imgHost + item
        })
        wx.previewImage({
            current: sources[photo_index],
            urls: sources
        })
    },
})